<?php

namespace App\Console\Commands;

use App\Console\Service\ZhihuService;
use Illuminate\Console\Command;
use App\Console\Service\ZhihuServie;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class MyCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'collect:zhihu {name=get}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'php artisan collect:zhihu {get|save|1|2}';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        define('HOST', '127.0.0.1');
        define('PORT', 5672);  // 注意这里端口号，在浏览器中的端口号是15672，但在这里确实5672
        define('USER', 'guest');
        define('PASS', 'guest');
        define('VHOST', '/');


        $ZhihuServie = new ZhihuService();
        $name = $this->argument('name');
        switch ($name){
            case 'get':
                $ZhihuServie->makeData();
                break;
            case 'save':
                $ZhihuServie->saveData();
                break;
            case '1':
                //If this is enabled you can see AMQP output on the CLI
                //如果为true会显示一堆<hex>这种类型的调试信息。
                define('AMQP_DEBUG', false);
                $exchange = 'router'; // 交换器，在我理解，如果两个队列使用一个交换器就代表着两个队列是同步的，这个队列里存在的消息，在另一个队列里也会存在
                $queue = 'test'; // 队列名称
                $connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST); // 创建连接
                $channel = $connection->channel();
                $channel->queue_declare($queue, false, true, false, false);
                $channel->exchange_declare($exchange, 'direct', false, true, false);
                $channel->queue_bind($queue, $exchange); // 队列和交换器绑定
                $messageBody = 'testinfo12'; // 要推送的消息
                $message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
                $channel->basic_publish($message, $exchange); // 推送消息
                $channel->close();
                $connection->close();
                break;
            case '2':
                $exchange = 'router';
                $queue = 'test';
                $connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);
                $channel = $connection->channel();
                $message = $channel->basic_get($queue); //取出消息
                print_r($message->body);
                $channel->basic_ack($message->delivery_info['delivery_tag']); // 确认取出消息后会发送一个ack来确认取出来了，然后会从rabbitmq中将这个消息移除，如果删掉这段代码，会发现rabbitmq中的消息还是没有减少
                $channel->close();
                $connection->close();
            default:
                return;
        }
    }
}
